package com.key.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.modules.dto.SysMenuSelectDTO;
import com.key.modules.entity.SysMenu;
import com.key.modules.entity.SysRole;
import com.key.modules.mapper.SysMenuMapper;
import com.key.modules.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author：朕
 * @Package：com.key.modules.service.impl
 * @Project：logistics_system
 * @name：SysMenuServiceImpl
 * @Date：2023/3/23 20:06
 * @Filename：SysMenuServiceImpl
 */
@Service
@Slf4j
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 显示权限
     * @return
     */
    @Override
    public IPage<SysMenu> findSysMenu(SysMenuSelectDTO sysMenuSelectDTO) {
        log.info("权限的显示-分页"+sysMenuSelectDTO);
        Page<SysMenu> page =new Page<>(sysMenuSelectDTO.getCurrent()-1,sysMenuSelectDTO.getSize());
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        IPage<SysMenu> iPage = sysMenuMapper.selectPage(page,wrapper);
        log.info("显示成功！"+iPage);
        return iPage;
    }

    /**
     * 增加权限
     * @param sysMenu
     * @return
     */
    @Override
    public Boolean addSysMenu(SysMenu sysMenu) {
        log.info("权限的增加："+sysMenu);
        int s1=0;
        Date date = new Date();
        sysMenu.setMenuDate(date);
        sysMenu.setMenuUpdateDate(null);
        SysMenu s = sysMenuMapper.findMenuByName(sysMenu.getMenuName());
        if(s==null){
             s1= sysMenuMapper.insert(sysMenu);
        }
        if(s1>0){
            log.info("增加成功:"+s1);
            return true;
        }
        log.info("添加失败:"+s1);
        return false;
    }

    /**
     * 修改权限
     * @param sysMenu
     * @return
     */
    @Override
    public Boolean updateSysMenu(SysMenu sysMenu) {
        int s = sysMenuMapper.updateById(sysMenu);
        if(s>0){
            log.info("修改成功:"+s);
            return true;
        }
        log.info("修改失败:"+s);
        return false;
    }

    /**
     * 删除权限
     * @param menuId
     * @return
     */
    @Override
    public Boolean deleteSysMenu(Integer menuId) {
        log.info("根据权限id删除权限：id-"+menuId);
        int s = sysMenuMapper.deleteById(menuId);
        if(s>0){
            log.info("删除成功:"+s);
            return true;
        }
        log.info("删除失败:"+s);
        return false;
    }
}
